package com.dly.blog.domain.vo;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.experimental.Accessors;
import org.springframework.beans.BeanUtils;

import java.util.List;
import java.util.function.Function;

/**
 * @Author: DaiLiYu
 * @Date: 2023/7/21 10:49
 */
@Data
@Accessors(chain = true)
@AllArgsConstructor
@NoArgsConstructor
public class PageVO<T>{

    private long current;

    private long total;

    private long size;

    private List<T> records;

    public static <T, R> PageVO<T> buildMyBatis(Page<R> page, Function<R, T> function){
        PageVO<T> pageVO = new PageVO<>();
        BeanUtils.copyProperties(page, pageVO);
        return pageVO.setRecords(page
                .getRecords()
                .stream().map(function).toList());
    }

    public static <T, R> PageVO<T> buildJPA(org.springframework.data.domain.Page<R> page, Function<R, T> function){
        PageVO<T> pageVO = new PageVO<>();
        pageVO.setCurrent(page.getNumber());
        pageVO.setSize(page.getSize());
        pageVO.setTotal(page.getTotalPages());
        return pageVO.setRecords(page.getContent().stream().map(function).toList());
    }

}
